module(..., package.seeall)


function string.split(s, pattern, init, plain, maxsplit)
    --[[ A Python-like string.split() function. Default split pattern splits strings by any amount 
    of whitespace. ]]
    if pattern == nil then
        pattern = "%s+"
    end
    local maxsplit = maxsplit or math.huge
    local parts = {}
    local i = init or 1
    while i <= #s  and #parts < maxsplit do
        local j, k = string.find(s, pattern, i, plain)
        if j == nil then
            break
        end
        table.insert(parts, string.sub(s, i, j-1))
        i = k + 1
    end
    table.insert(parts, string.sub(s, i, #s))
    return parts
end

function string.join(s, parts, i, j)
    return table.concat(parts, s, i, j)
end

function string.startswith(s, prefix)
    return #s >= #prefix and s:sub(1, #prefix) == prefix
end

function string.endswith(s, suffix)
    return #s >= #suffix and s:sub(-#suffix) == suffix
end

function string.capitalize(s)
	local parts = {}
	for before, char, after in string.gmatch(s, "([^%w]*)(%w)(%w*)") do
		table.insert(parts, before)
		table.insert(parts, char:upper())
		table.insert(parts, after)
	end
	local new_s = (""):join(parts)
	if #s > #new_s then
		new_s = new_s .. string.sub(s, #new_s + 1)
	end
	return new_s
end
